package com.koron.common.core.business.print;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.util.RedisUtils;
import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.common.core.business.print.bean.PrintTemplateBean;
import com.koron.common.core.business.print.bean.ReportTaskBean;
import com.koron.common.core.business.print.printstandard.ReportServiceStandardImpl;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.exception.BaseException;
import com.koron.common.core.exception.SystemErrorType;
import com.koron.common.core.util.FastJsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
import java.util.*;

@Slf4j
public class ReportTask {

    /**
     * 单机版本缓存
     */
    public static HashMap<String, ReportTaskBean> task = new HashMap<String, ReportTaskBean>();
    public static TimedCache<String, ReportTaskBean> printTaskTimedCache = CacheUtil.newTimedCache(1000 * 60 * 1440);

    static {
        printTaskTimedCache.schedulePrune(1000 * 60 * 10);//启动定时任务，每十分钟检查一次
    }

    public static String printData(String printTemplateId, List<?> list) {
        return printData(ThreadLocalContext.get(), printTemplateId, list);
    }

    /**
     * 获取资源地址
     *
     * @author dulingjiang
     * @date 2020年8月10日
     */
    public static String printData(EamUser user, String printTemplateId, List<?> list) {
        ArrayList<HashMap<String, String>> data = FastJsonUtils.convertToDataList(list);
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        ReportTaskBean taskBean = new ReportTaskBean();
        taskBean.setTaskType(ReportTaskBean.TASKTYPE_PRINT);
        taskBean.setTimeStamp(new Date().getTime());//记录时间戳
        ReportServiceStandardImpl reportSI = new ReportServiceStandardImpl();

        PrintTemplateBean printtemplate = new PrintTemplateBean();
        printtemplate.setEamuser(user);
        printtemplate.setId(printTemplateId);
        reportSI.setPrintTemplate(printtemplate);
        taskBean.setReportServiceInterface(reportSI);
        taskBean.setPrintData(data);

        if (false) {//集群分布
            String json = null;
            try {
                json = new ObjectMapper().writeValueAsString(taskBean);
            } catch (JsonProcessingException e) {
                log.error(e.getMessage(), e);
                throw new BaseException(SystemErrorType.SYSTEM_ERROR);
            }
            setPrintData("Print-" + key, json);
        }
        //找不到 redis的ping方法 暂时写true
        if (true) {//单机redis
//            RedisUtils.set(key, FastJsonUtils.getBeanToJson(taskBean), 10 * 60);//10分钟过期
            try {
                setPrintData("Print-" + key, new ObjectMapper().writeValueAsString(taskBean));
            } catch (JsonProcessingException e) {
                log.error(e.getMessage(), e);
                throw new BaseException(SystemErrorType.SYSTEM_ERROR);
            }
        }
//        ReportTask.task.put("Print-" + key, taskBean);弃用
        printTaskTimedCache.put("Print-" + key, taskBean);
        Map<String, String> configByCode = ConfigInfoTool.getConfigByCode("common", "print");
        String url = configByCode.get("url");
        String host = configByCode.get("host");
        String printUrl = MessageFormat.format(url, PrintConst.TYPE_VIEW, key, host, key);
        return printUrl;
    }


    /**
     * 打印数据缓存
     *
     * @param id
     * @param value
     */
    public static void setPrintData(String id, String value) {
        RedisUtils.set(id, value, 1440 * 60);
    }


    public static String getPrintData(String id) {
        return RedisUtils.get(id).toString();
    }
}
